package cn.zucc.edu.bruce.iot.receiver;

import android.util.Log;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.zucc.edu.bruce.iot.DataDeal;

public class ReceiveIOTData implements Serializable {
    private int header;
    private String sno;
    private short heartbeat;
    private char iotId;
    private int sensorNum;
    private List<ReceiveSensorData> receiveSensorData = new ArrayList<>();
    private char checksum;
    private int end;

    public ReceiveIOTData() {
        this.header = 0xfffffffe;
        this.end = 0xeffffffe;
    }

    public String getSno() {
        return sno;
    }

    public void setSno(byte[] bytes) {
        this.sno = DataDeal.bytesToString(bytes, 8);
    }

    public short getHeartbeat() {
        return heartbeat;
    }

    public void setHeartbeat(byte[] bytes) {
        this.heartbeat = DataDeal.bytesToShort(bytes);
    }

    public char getIotId() {
        return iotId;
    }

    public void setIotId(byte b) {
        this.iotId = DataDeal.byteToChar(b);
    }

    public char getChecksum() {
        return checksum;
    }

    public void setChecksum(byte[] bytes) {
        this.checksum = (char) (bytes[0] & 0xff);
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(byte[] bytes) {
        this.end = DataDeal.bytesToInt(bytes, 0);
    }

    public int getSensorNum() {
        return sensorNum;
    }

    public void setSensorNum(byte[] bytes) {
        this.sensorNum = DataDeal.bytesToInt(bytes, 0);
    }

    public List<ReceiveSensorData> getReceiveSensorData() {
        return receiveSensorData;
    }


    public void addSensor(byte[] bytes) {
        int sensorNumber = DataDeal.byteToChar(bytes[0]);
        byte[] b = new byte[12];
        System.arraycopy(bytes, 1, b, 0, 12);
        ReceiveSensorData receiveSensorData = null;
        switch (sensorNumber) {
            case 0:
                receiveSensorData = new ReceiveTemperHumData(b);
                break;
            case 1:
                receiveSensorData = new ReceiveRgbData(b);
                break;
            case 2:
                receiveSensorData = new ReceiveInfraredData(b);
                break;
            case 3:
                receiveSensorData = new ReceiveMotorData(b);
                break;
        }
        this.receiveSensorData.add(receiveSensorData);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder("Received: sno: " + this.getSno() + " heartbeat: " + this.getHeartbeat() + " iotId: " + (int) this.getIotId() + " sensorNum: " + this.getSensorNum() + " ");
        for (ReceiveSensorData s : this.getReceiveSensorData())
            res.append(s.toString()).append(" ");
        res.append("checkSum: ").append(this.getChecksum());
        return res.toString();
    }
}

